package com.hzit.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Configuration
@MapperScan("com.hzit.mapper")
@Slf4j
public class MybatisPlusConfig implements MetaObjectHandler {


	//1. 插入时自动填充处理
	@Override
	public void insertFill(MetaObject metaObject) {
		log.info("insertFill....");
		this.setFieldValByName("createTime",LocalDateTime.now(),metaObject);
		this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
	}
	//2. 修改时自动填充的处理逻辑
	@Override
	public void updateFill(MetaObject metaObject) {
		log.info("updateFill....");
		this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
	}

	//3. 乐观锁配置
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
		return interceptor;
	}
}
